import 'package:enterprise_v6_app/app/components/special_operation/custom_high_input_page.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_multi_signature_page.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_picker_photo.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_gas_page.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/application/execute_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/special_operation/special_list/job_ticket/common/common_process/common_gas_analysis/common_sampling_analysis/common_sampling_analysis_controller.dart';

///作业通用-气体分析-取样分析
class CommonSamplingAnalysisPage extends StatelessWidget {
  CommonSamplingAnalysisPage({required this.isEnabled, required this.businessType, super.key});

  // 节点类型 1：动火作业 2：受限空间作业 3：盲板抽堵作业 4：高处作业 5：吊装作业 6：临时用电作业 7：动土作业 8：断路作业
  final String businessType;

  //节点是否可编辑
  final bool isEnabled;

  final controller = Get.put(CommonSamplingAnalysisController());

  /// 根据不同作业类型,需要采集不同的数据
  Widget _getCollectionView(ExecuteTicketGasList item) {
    switch (businessType) {
      // 动火作业
      case '1':
        return _getFireWork(item);
      // 受限空间
      case '2':
        return _getRestrictedView(item);
      // 临时用电
      case '6':
        return _getElectricityWork(item);
      default:
        return Container();
    }
  }

  /// 动火作业
  Widget _getFireWork(ExecuteTicketGasList item) {
    return CustomSelectGasPage(
      isEnabled: item.isNewData && isEnabled,
      title: '检测气体',
      isShowTitle: false,
      gasList: item.workGasJsonDTOList!,
      onCallBack: (value) {},
    );
  }

  /// 受限空间
  Widget _getRestrictedView(ExecuteTicketGasList item) {
    return Column(
      children: [
        CustomSelectGasPage(
          isEnabled: item.isNewData && isEnabled,
          title: '有毒有害气体',
          isShowGasName: false,
          gasList: item.workGasJsonDTOList!,
          onCallBack: (value) {},
        ),
        CustomSelectGasPage(
          isEnabled: item.isNewData && isEnabled,
          title: '可燃气体',
          isShowGasName: false,
          gasList: item.workGasJsonDTOList!,
          onCallBack: (value) {},
        ),
        CustomSelectGasPage(
          isEnabled: item.isNewData && isEnabled,
          title: '氧气含量',
          isShowGasName: false,
          gasList: item.workGasJsonDTOList!,
          onCallBack: (value) {},
        ),
      ],
    );
  }

  /// 临时用电
  Widget _getElectricityWork(ExecuteTicketGasList item) {
    return CustomSelectGasPage(
      isEnabled: item.isNewData && isEnabled,
      title: '可燃气体检测结果',
      isShowGasName: false,
      gasList: item.workGasJsonDTOList!,
      onCallBack: (value) {},
    );
  }

  ///获取气体分析显示
  Widget _getSamplingAnalysisView(ExecuteTicketGasList item, int index) {
    return Container(
      margin: EdgeInsets.symmetric(vertical: 7.w),
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.w),
      ),
      child: Column(
        children: [
          CustomSelectPage(
              isEnabled: item.isNewData && isEnabled,
              title: '取样分析时间',
              selectText: item.sampleDate ?? '',
              onCallBack: () {
                controller.showDatePicker(item);
              }),
          CustomHighInputPage(
            isEnabled: item.isNewData && isEnabled,
            title: '分析点名称',
            text: item.addressName,
            onCallBack: (value) {
              item.addressName = value;
            },
            isShowDivider: true,
          ),
          _getCollectionView(item),
          CustomMultiSignaturePage(
            isEnabled: item.isNewData && isEnabled,
            title: '分析人',
            signatureLists: item.sampleUserSign ?? [],
            onCallBack: (value) {
              item.sampleUserSign = value;
            },
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return SingleChildScrollView(
            child: Column(
              children: [
                ListView.separated(
                  itemCount: controller.ticketGasList.length,
                  shrinkWrap: true,
                  physics: const NeverScrollableScrollPhysics(),
                  itemBuilder: (context, index) {
                    return Container(
                      alignment: Alignment.center,
                      child: Stack(
                        alignment: Alignment.topRight,
                        children: [
                          //获取作业人信息
                          _getSamplingAnalysisView(controller.ticketGasList[index], index),
                          //删除按钮
                          Visibility(
                              visible: controller.ticketGasList[index].isNewData && isEnabled,
                              child: const Icon(
                                Icons.highlight_remove,
                                color: Color(0xFFFD6B6B),
                              ).clickOnce(() {
                                controller.deleteGasAnalysis(controller.ticketGasList[index]);
                              }))
                        ],
                      ),
                    );
                  },
                  separatorBuilder: (BuildContext context, int index) {
                    return AppGaps.vGap8;
                  },
                ),
                Visibility(
                    visible: isEnabled,
                    child: Column(
                      children: [
                        AppGaps.vGap20,
                        Container(
                          height: 40.w,
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(8.w),
                          ),
                          child: Text(
                            '新增气体分析',
                            style: TextStyle(
                              color: const Color(0xFF5C6DFF),
                              fontWeight: FontWeight.w400,
                              fontSize: 12.sp,
                            ),
                          ),
                        ).clickOnce(controller.addGasAnalysis),
                      ],
                    )),
                AppGaps.vGap20,
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 16.w),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(8.w),
                  ),
                  child: CustomPickerPhoto(
                    isEnabled: isEnabled,
                    imagesList: controller.workAllPageController.executeEntity.gasUrl!,
                    title: '分析结果上传',
                    callback: (List<String> path) {
                      controller.workAllPageController.executeEntity.gasUrl = path;
                    },
                  ),
                ),
              ],
            ),
          );
        });
  }
}
